package com.cj.mobile.iabp.mvp.model;

import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.mvp.contract.AddPatientContract;
import com.cj.mobile.iabp.mvp.model.api.service.AccountService;
import com.cj.mobile.iabp.mvp.model.entity.DeptInfo;
import com.cj.mobile.iabp.mvp.model.entity.DictInfo;
import com.cj.mobile.iabp.mvp.model.entity.PatientInfo;
import com.jess.arms.cj.ApiOperator;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2025/09/24 15:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class AddPatientModel extends BaseModel implements AddPatientContract.Model {
    @Inject
    ApiOperator apiOperator;                                                                        // 数据转换

    @Inject
    AccountManager mAccountManager;


    @Inject
    public AddPatientModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.apiOperator = null;
        this.mAccountManager = null;
    }

    @Override
    public Observable<List<DeptInfo>> getDeptList(String parentId) {
        Map<String, Object> params = new HashMap<>();
        params.put("parentId", parentId);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).getDeptList(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<List<DictInfo>> getDictList() {
        Map<String, Object> params = new HashMap<>();
        params.put("type", 1);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).getDictList(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<Integer> updatePatient(String per, String id, String deptSec, String cnt, String code, String name, String height, String weight, String sex, String age, String diagnoseF, String diagnose, String diagnoseS, String deptId, String area, String bedNo, String friends, String phone) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("per", per);
        params.put("deptSec", deptSec);
        params.put("cnt", cnt);
        params.put("code", code);
        params.put("name", name);
        params.put("height", height);
        params.put("weight", weight);
        params.put("sex", sex);
        params.put("age", age);
        params.put("diagnoseF", diagnoseF);
        params.put("diagnose", diagnose);
        params.put("diagnoseS", diagnoseS);
        params.put("deptId", deptId);
        params.put("area", area);
        params.put("bedNo", bedNo);
        params.put("friends", friends);
        params.put("phone", phone);
        params.put("doctorId", mAccountManager.getUserid());

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).updatePatient(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<PatientInfo> addPatient(String per, String deptSec, String cnt, String code, String name, String height, String weight,
                                              String sex, String age, String diagnoseF, String diagnose, String diagnoseS, String deptId,
                                              String area, String bedNo, String friends, String phone) {
        Map<String, Object> params = new HashMap<>();
        params.put("per", per);
        params.put("deptSec", deptSec);
        params.put("cnt", cnt);
        params.put("code", code);
        params.put("name", name);
        params.put("height", height);
        params.put("weight", weight);
        params.put("sex", sex);
        params.put("age", age);
        params.put("diagnoseF", diagnoseF);
        params.put("diagnose", diagnose);
        params.put("diagnoseS", diagnoseS);
        params.put("deptId", deptId);
        params.put("area", area);
        params.put("bedNo", bedNo);
        params.put("friends", friends);
        params.put("phone", phone);
        params.put("doctorId", mAccountManager.getUserid());

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).addPatient(mAccountManager.getToken(), request));
    }
}